package com.example.my.agiftthat.ui.shop;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.my.agiftthat.MvpActivity.ClickSearchActivitys;
import com.example.my.agiftthat.R;
import com.example.my.agiftthat.helper.RecyclerViewAdapterHelper;
import com.example.my.agiftthat.ui.list.intentlist.ListIntentActivity;
import com.example.my.agiftthat.ui.shop.intentactivity.IntentActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by My on 2016/12/21.
 */

public class ShopAdapter extends RecyclerViewAdapterHelper<ShopBean.DataBean.ItemsBeanX> {
    public static final int ONE = 0, THW = 1;

    public ShopAdapter(Context context, List<ShopBean.DataBean.ItemsBeanX> list) {
        super(context, list);
    }

    @Override
    public int getItemViewType(int position) {
        if (!(list.get(position).getImage_url() == null)) {
            return ONE;
        } else {
            return THW;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case ONE:
                view = inflater.inflate(R.layout.item_shop2, parent, false);
                return new ViewHolder(view);
            case THW:

                view = inflater.inflate(R.layout.item_shop1, parent, false);
                return new ViewHolder1(view);
        }
        return null;
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ClickSearchActivitys.class);
                    intent.putExtra("id",list.get(position).getWebp_url());
                    context.startActivity(intent);
                }
            });
            Glide.with(context).load(list.get(position).getImage_url()).into(((ViewHolder) holder).itemItemImage);

        } else if (holder instanceof ViewHolder1) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, IntentActivity.class);
                    context.startActivity(intent);
                }
            });
//            GridLayoutManager manager = new GridLayoutManager(mContext, 3);
//            manager.setOrientation(GridLayoutManager.HORIZONTAL);
            ((ViewHolder1) holder).item2ItemText.setText(list.get(position).getTitle());
            Glide.with(context).load(list.get(position).getCover_image_url()).into(((ViewHolder1) holder).item2ItemImage);
            LinearLayoutManager manager = new LinearLayoutManager(context);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            ((ViewHolder1) holder).item2ItemRecyclerView.setLayoutManager(manager);
            ItemAdapter adapter = new ItemAdapter(context,list.get(position).getItems());
            ((ViewHolder1) holder).item2ItemRecyclerView.setAdapter(adapter);
        }
    }




    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_item_image)
        ImageView itemItemImage;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    static class ViewHolder1 extends RecyclerView.ViewHolder {
        @Bind(R.id.item2_item_image)
        ImageView item2ItemImage;
        @Bind(R.id.item2_item_text)
        TextView item2ItemText;
        @Bind(R.id.item2_item_recyclerView)
        RecyclerView item2ItemRecyclerView;

        ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
